package com.lite.ui;

import java.sql.*;
import javax.swing.table.*;
import java.util.Vector;
import javax.swing.*;

class ResultsModel extends AbstractTableModel {

	/**
     * 
     */
	private static final long serialVersionUID = 6707027182873294943L;
	String[] columnNames = new String[0];
	Vector<String[]> dataRows; // Empty vector of rows

	Vector<ResultSet> resultsVector = new Vector<ResultSet>();
	ResultSet gResults;
	int gColumns;
	String[] gRowData;
	int arraySize = 100;
	int totalRowCount = 0;
	
	public Vector<ResultSet> getResultSet(){
		return resultsVector;
	}
	
	public void setResultSet(Vector<ResultSet> results) {
		try {
			if (results == null) {
				gColumns = 0;
				totalRowCount = 0;
				columnNames = new String[0];
				fireTableChanged(null);
				return;
			}
			resultsVector = results;
			ResultSet result = resultsVector.get(0);

			ResultSetMetaData metadata = result.getMetaData();

			int columns = metadata.getColumnCount(); // Get number of columns

			gColumns = columns;

			columnNames = new String[columns]; // Array to hold names
			totalRowCount = 0;

			// Get the column names
			for (int i = 0; i < columns; i++)
				columnNames[i] = metadata.getColumnLabel(i + 1);

			// Get all rows.
			dataRows = new Vector<String[]>(); // New Vector to store the data
			String[] rowData; // Stores one row
			for (int i = 0; i < resultsVector.size(); i++) {
				result = resultsVector.get(i);
				while (result.next()) // For each row...
				{
					rowData = new String[columns]; // create array to hold the
													// data
					for (int j = 0; j < columns; j++)
						// For each column
						rowData[j] = result.getString(j + 1); // retrieve the
																// data item

					dataRows.addElement(rowData); // Store the row in the vector
					totalRowCount++;
					if (result.getRow() == arraySize)
						break;
				}
				result.beforeFirst();

				fireTableChanged(null); // Signal the table there is new model
										// data

			}
		} catch (SQLException sqle) {
			System.err.println(sqle);
		}

	}

	public void setResultSet(ResultSet results) {
		try {
			if (results == null) {
				gColumns = 0;
				totalRowCount = 0;
				columnNames = new String[0];
				fireTableChanged(null);
				return;
			}
			gResults = results;

			ResultSetMetaData metadata = results.getMetaData();

			int columns = metadata.getColumnCount(); // Get number of columns

			gColumns = columns;

			columnNames = new String[columns]; // Array to hold names
			totalRowCount = 0;

			// Get the column names
			for (int i = 0; i < columns; i++)
				columnNames[i] = metadata.getColumnLabel(i + 1);

			// Get all rows.
			dataRows = new Vector<String[]>(); // New Vector to store the data
			String[] rowData; // Stores one row
			while (results.next()) // For each row...
			{
				rowData = new String[columns]; // create array to hold the data
				for (int i = 0; i < columns; i++)
					// For each column
					rowData[i] = results.getString(i + 1); // retrieve the data
															// item

				dataRows.addElement(rowData); // Store the row in the vector
				totalRowCount++;
				if (results.getRow() == arraySize)
					break;
			}

			fireTableChanged(null); // Signal the table there is new model data
		} catch (SQLException sqle) {
			System.err.println(sqle);
		}

	}

	public void setNextResultSet(JTable table) {
		try {
			int lastRow = gResults.getRow() + arraySize;
			int[] colWidth = new int[gColumns];

			while (gResults.next()) // For each row...
			{
				gRowData = new String[gColumns]; // create array to hold the
													// data
				for (int i = 0; i < gColumns; i++)
					// For each column
					gRowData[i] = gResults.getString(i + 1); // retrieve the
																// data item

				dataRows.addElement(gRowData); // Store the row in the vector
				totalRowCount++;
				if (gResults.getRow() == lastRow)
					break;
			}

			for (int i = 0; i < gColumns; i++) {
				colWidth[i] = table.getColumnModel().getColumn(i).getWidth();
			}

			fireTableChanged(null);

			for (int i = 0; i < gColumns; i++) {
				table.getColumnModel().getColumn(i).setWidth(colWidth[i]);
			}
		} catch (SQLException sqle) {
			System.err.println(sqle);
		}

	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		// if(dataRows == null)
		// return 0;
		// else
		// return dataRows.size();
		return totalRowCount;
	}

	public Object getValueAt(int row, int column) {
		return ((String[]) (dataRows.elementAt(row)))[column];
	}

	public String getColumnName(int column) {
		return columnNames[column] == null ? "No Name" : columnNames[column];
	}

}
